<?php

namespace app\admin\controller\call;

use app\common\controller\Backend;

use app\admin\model\Gateway;

/**
 * 话费详单管理
 *
 * @icon fa fa-circle-o
 */
class Cdr extends Backend
{
    
    /**
     * Cdr模型对象
     * @var \app\admin\model\Cdr
     */
    protected $model = null;

    protected $searchFields = 'caller_id_name,caller_id_number,gateway,destination_number';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Cdr');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    protected function getChildrenGatewayNames($withself = false) {
        $gatewayNames = [];
        $adminIds = $this->auth->getChildrenAdminIds($withself);
        $gatewayList = collection(Gateway::field('name')->where('admin_id', 'in', $adminIds)->select())->toArray();
        foreach ($gatewayList as $k => $v) {
            $gatewayNames[] = $v['name'];
        }
        return $gatewayNames;
   }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where2 = array('context' => array('in', 'incall,outcall'));
            $where3 = array();
            if (! $this->auth->isSuperAdmin()) {
                $gatewayNames = $this->getChildrenGatewayNames(true);
                $where3 = array('gateway' => array('in', $gatewayNames));
            }
            $total = $this->model
                    ->where($where)
                    ->where($where2)
                    ->where($where3)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where($where2)
                    ->where($where3)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $list = collection($list)->toArray();
            foreach($list as &$v) {
                if ($v['context'] == 'incall') {
                    $v['billmin'] = 0;
                    $v['peer_number'] = $v['caller_id_number'];
                    continue;
                } else {
                    $v['peer_number'] = $v['destination_number'];
                }
                if ($v['billsec'] < 1) {
                    $v['billmin'] = 0;
                    continue;
                }
                $v['billmin'] = (int)($v['billsec'] / 60) + ($v['billsec'] % 60 > 0) ? 1 : 0;
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function add() {
        $this->error(__('You have no permission'));
    }

    public function edit($ids = NULL) {
        $this->error(__('You have no permission'));
    }

    public function del($ids = "") {
        $this->error(__('You have no permission'));
    }

    public function multi($ids = "") {
        $this->error(__('You have no permission'));
    }

}
